﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WB.Entity;
using WB.Entity.Helper;
using WB.DaoHelper;
using WB.Library;
using Bussiness.Manager;
using Bussiness.Entity;



public partial class Admin_News_AddEdit : System.Web.UI.UserControl
{
    public Guid NewsId
    {
        //get { return FGuid.ToGuid(Request["id"]); }

        get
        {
            if (FGuid.IsGuid(hidNewsId.Value))
                return new Guid(hidNewsId.Value);
            return Guid.Empty;
        }
        set
        {
            hidNewsId.Value = value.ToString();
        }
    }

    public string Language
    {
        get { return hidLang.Value; }
        set
        {
            hidLang.Value = value;
           
        }
    }

    public Guid NewsInfoId
    {
        get
        {
            if (FGuid.IsGuid(hidNewsInfo.Value))
                return new Guid(hidNewsInfo.Value);
            return Guid.Empty;
        }
        set
        {
            hidNewsInfo.Value = value.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

           hidNewsId.Value = Request["id"];
           if (hidNewsId.Value == "")
               btnAddNews.Text = "Thêm mới";
           else
               btnAddNews.Text = "Cập nhật";
           LoadParent();
           LoadDetail();
             
        }
    }

    protected void LoadParent()
    {
        ddlVCategory.Items.Clear();
       

        string catType = "NEWS";
        if (Request["control"] == "editintro")
        {
            catType = "INTR";
            btnBack.Visible = false;
        }
        else if (Request["control"] == "addbranch")
            catType = "BRAN";
        else
        {
            ddlVCategory.Items.Add(new ListItem("--Không chọn--", ""));
            ddlVCategory.AppendDataBoundItems = true;
        }
        DataTable tbl = CmsCategoryManager.CreateInstant().GetCategoryTreeByLang(catType, Language);
      
        ddlVCategory.DataSource = tbl;
        ddlVCategory.DataTextField = "CatName";
        ddlVCategory.DataValueField = "CategoryId";
        ddlVCategory.DataGroupField = "ParentName";
        ddlVCategory.DataBind();
        if (catType != "NEWS")
            ddlVCategory.Enabled = false;
    }

    protected void LoadDetail()
    {
        if (NewsId != Guid.Empty)
        {
            CmsNewsInfosEntity objNewsF = new CmsNewsInfosEntity();
            objNewsF.NewsId = NewsId;
            objNewsF.Lang = Language;
            DataTable tbl = CmsNewsInfosManager.CreateInstant().SelectWithCondition(objNewsF);
            if (tbl.Rows.Count > 0)
            {

                hidNewsInfo.Value = tbl.Rows[0]["Id"].ToString();
                txtTitle.Text = tbl.Rows[0]["Title"].ToString();
                txtContent.Value = tbl.Rows[0]["ContentBody"].ToString();
                txtInfor.Text = tbl.Rows[0]["Absolute"].ToString();
                if (tbl.Rows[0]["NewsId"].ToString() != "")
                {
                    DataTable dtNews = CmsNewsManager.CreateInstant().SelectById(new Guid(tbl.Rows[0]["NewsId"].ToString()));
                    if (dtNews.Rows.Count > 0)
                    {
                        if (dtNews.Rows[0]["CategoryId"].ToString() != "")
                        {
                            ddlVCategory.SelectedValue = dtNews.Rows[0]["CategoryId"].ToString();
                        }
                        if (!string.IsNullOrEmpty(dtNews.Rows[0]["Photo"].ToString()))
                        {
                            imgPhoto.Visible = true;
                            imgPhoto.ImageUrl = dtNews.Rows[0]["Photo"].ToString();
                        }
                    }
                }
            }
        }
    }

    protected void Insert()
    {
        
        CmsNewsEntity objNews = new CmsNewsEntity();
        objNews.Id = Guid.NewGuid();
        if (FGuid.IsGuid(ddlVCategory.SelectedValue))
            objNews.CategoryId = new Guid(ddlVCategory.SelectedValue);
        if (filePhoto.HasFile)
            objNews.Photo = FileUploadControl.FullPath(filePhoto ,NewsField.Photo.MaxLength);

        
        objNews.CreatedBy = Profile.UserName;
        objNews.CreatedDate = DateTime.Now;

        CmsNewsManager.CreateInstant().Insert(objNews);

        NewsId = objNews.Id;
        hidNewsId.Value = NewsId.ToString();

       
        CmsNewsInfosEntity objNewsInfo = new CmsNewsInfosEntity();
        objNewsInfo.Id = Guid.NewGuid();
        objNewsInfo.NewsId = NewsId;
        objNewsInfo.Lang = Language;
        objNewsInfo.Title = txtTitle.Text;
        objNewsInfo.Absolute = txtInfor.Text;
        objNewsInfo.ContentBody = txtContent.Value.ToString();
        objNewsInfo.CreatedBy = Profile.UserName;
        objNewsInfo.CreatedDate = DateTime.Now;

        CmsNewsInfosManager.CreateInstant().Insert(objNewsInfo);
      
        CmsNewsInfosEntity objNewsInfo1 = new CmsNewsInfosEntity();
        objNewsInfo1.Id = Guid.NewGuid();
        objNewsInfo1.NewsId = NewsId;
        objNewsInfo1.Lang = Language.ToLower() == "vi-vn" ? "en-US" : "vi-VN";
        objNewsInfo1.CreatedBy = Profile.UserName;
        objNewsInfo1.CreatedDate = DateTime.Now;

        CmsNewsInfosManager.CreateInstant().Insert(objNewsInfo1);
        
        Response.Redirect("?control=NucNews&id=" + NewsId.ToString());
        CustomValidator1.ErrorMessage = "Đã thêm dữ liệu thành công.";
        CustomValidator1.IsValid = false;

        switch (Request["control"])
        {
            case "NEWS": Response.Redirect("?control=editintro&id=" + NewsId.ToString());
                break;
            case "INTR": 
                break;
            case "BRAN": Response.Redirect("?control=addbranch&id=" + NewsId.ToString());
                break;
        }
 
        
    }

    protected void Update()
    {
        CmsNewsInfosEntity obj = new CmsNewsInfosEntity();
        obj.Id = NewsInfoId;
        obj.Title = txtTitle.Text;
        obj.Absolute = txtInfor.Text;
        obj.ContentBody = txtContent.Value.ToString();
        obj.UpdatedBy = Profile.UserName;
        obj.UpdatedDate = DateTime.Now;

        CmsNewsInfosManager.CreateInstant().Update(obj);

        CmsNewsEntity obNews = new CmsNewsEntity();
        obNews.Id = NewsId;
        if(ddlVCategory.SelectedValue != "")
            obNews.CategoryId = new Guid(ddlVCategory.SelectedValue);
        if (filePhoto.HasFile)
            obNews.Photo = FileUploadControl.FullPath(filePhoto, NewsField.Photo.MaxLength);
        if (ddlVCategory.SelectedValue != "" || filePhoto.HasFile)
            CmsNewsManager.CreateInstant().Update(obNews);
        
        CustomValidator1.ErrorMessage = "Đã cập nhật dữ liệu thành công.";
        CustomValidator1.IsValid = false;

    }

    protected void btnAddNews_Click(object sender, EventArgs e)
    {
        if (hidNewsId.Value == "")
        {
            
            Insert();
            btnAddNews.Text = "Cập nhật";
            

        }
        else
        {
            btnAddNews.Text = "Cập nhật";
            Update();
        }
        
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        switch (Request["control"])
        {
           
            case "editintro":
                break;
            case "addbranch": Response.Redirect("?control=branch");
                break;
            default: Response.Redirect("?control=nlist");
                break;
        }
      
    }
}
